package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ProfileResult;
import com.itheima.common.R;
import com.itheima.common.Roles;
import com.itheima.mapper.FrameMapper;
import com.itheima.pojo.*;
import com.itheima.service.FrameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.UUID;

@Service
public class FrameServiceImpl extends ServiceImpl<FrameMapper, BsUser> implements FrameService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FrameMapper frameMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public R login(String username,String password) {
        // 1.检查参数
        if (username == null || password ==null) {
            return R.error("参数异常");
        }
        //3.密码md5加密，进行判断
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("密码密码密码密码"+pwd);
        //4.调用数据库查询有没有这个用户
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsUser::getEmail,username);
        BsUser bsUser = frameMapper.selectOne(wrapper);
        if (null == bsUser) {
            return R.error("用户不存在");
        }
        if(!bsUser.getPassword().equals(password)){
            return R.error("密码不匹配");
        }
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set("userId",bsUser.getId());
        HttpSession session = request.getSession();
        session.setAttribute("userId",bsUser.getId());
        redisTemplate.opsForValue().set("token",token);
        R r = new R();
        r.setToken(token);
        return r;
    }

    @Override
    @Transactional
    public R updatePassword(String oldPassword, String newPassword) {
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsUser::getPassword,oldPassword);
        BsUser bsUser = frameMapper.selectOne(wrapper);
        bsUser.setPassword(newPassword);
        frameMapper.updateById(bsUser);
        return R.success("修改成功");
    }

    @Override
    public ProfileResult profile() {
        ProfileResult profileResult = new ProfileResult();

        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        BsUser bsUser = frameMapper.selectById(userId);
        if(bsUser == null){
            return profileResult;
        }
        System.out.println("1111111111111111111111111"+bsUser);
        profileResult.setAvatar(bsUser.getAvatar());
        profileResult.setIntroduction(bsUser.getIntroduction());
        profileResult.setName(bsUser.getUsername());
        profileResult.setRoles(new Roles());
        ArrayList<String> role = new ArrayList<>();
        role.add(bsUser.getRole());
        profileResult.setRole(role);
        return profileResult;
    }


    @Override
    public R regist(String username, String password) {
        //接受参数判断
        if(username == null || password == null){
            return R.error("参数异常");
        }

        BsUser bsUser = new BsUser();
        bsUser.setCreateTime(LocalDateTime.now());
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        bsUser.setPassword(pwd);
        bsUser.setEmail(username);
        bsUser.setSex("男");
        bsUser.setStatus(0);

        int insert = frameMapper.insert(bsUser);
        if (insert <= 0){
            return R.error("注册失败");
        }
        return R.success("注册成功");
    }

}